package codeRandomThoughts.Test111二叉树的最小深度;

import codeRandomThoughts.utils.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class Solution {
    public int minDepth(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        int deep=0;
        if (root == null)
            return deep;

        //记录每层节点个数
        int npl = 0; //number per level
        TreeNode node = root;
        queue.offer(node);
        while (!queue.isEmpty()) {
            npl = queue.size();
            deep++;

            while (npl > 0) {
                node = queue.poll();
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
                //只要出现叶子结点,直接返回
                if (node.left==null && node.right==null){
                    return deep;
                }
                npl--;
            }
        }
        return deep;
    }
}
